import { WS_ADDRESS } from "../config";

function useWebSocket(handleMessage) {
    // test id
    const user_id = 1;
    const webSocketUrl = WS_ADDRESS + "/" + user_id + "/";
    const ws = new WebSocket(webSocketUrl);

    const init = () => {
        bindEvent();
    }

    function bindEvent () {
        ws.addEventListener("open", handleOpen, false);
        ws.addEventListener("close", handleClose, false);
        ws.addEventListener("error", handleError, false);
        ws.addEventListener("message", handleMessage, false);
    }
    function handleOpen(e) {
        console.log("ws open", e);
    }
    function handleClose(e) {
        console.log("ws close", e);
    }
    function handleError(e) {
        console.log("ws error", e);
    }

    init();

    return ws;
}

export default useWebSocket;